package com.erp.erp_ui.Customer2Bank;

import com.erp.erp_entitys.Customer2Bank.Customer2BankEntity;
import com.erp.erp_entitys.Customer2Bank.Customer2BankEntityExample;
import com.erp.erp_entitys.Customer2Log.Customer2LogEntity;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.Customer2Bank.ICustomer2BankService;
import com.erp.erp_servers.Customer2Log.ICustomer2LogService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.DateUtils;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 客户
 * @ClassName Customer2BankControl
 * @Description TODO
 * @Author songjizhen
 * @Date 2020-07-03 23:07
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "customer2bank")
public class Customer2BankControl  extends Base {
    @Autowired
    ICustomer2BankService customer2BankService;
    @Autowired
    ICustomer2LogService customerLogService;
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    public R<Integer> delete(String[] ids){
        try {
            List<Integer> list=new ArrayList<>();
            String codes="";
            for(int i=0;i<ids.length;i++){
                list.add(Integer.parseInt(ids[i].replace("[","").replace("]","")));
                codes+=   ids[i].replace("[","").replace("]","")+",";
            }
            Customer2BankEntityExample example=new Customer2BankEntityExample();
            Customer2BankEntityExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(list);
            int num = customer2BankService.deleteByExample(example);
            //日志
            UserEntity user = (UserEntity)this.getBean("user");
            Customer2LogEntity record=new Customer2LogEntity();
            record.setCode(codes);
            record.setType("删除");
            record.setOptioner(user.getCode());
            record.setOptionTime(DateUtils.format(new Date()));
            record.setContent("编码："+codes);
            customerLogService.insert(record);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
          return   R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 新增银行信息
     * @param record
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
    public R<Integer> insert(@RequestBody Customer2BankEntity record){
        try {

                UserEntity user = (UserEntity)this.getBean("user");
                int num = customer2BankService.insertSelective(record);

                //日志
                Customer2LogEntity log=new Customer2LogEntity();
                log.setCode(record.getCode());
                log.setType("新增");
                log.setOptioner(user.getCode());
                log.setOptionTime(DateUtils.format(new Date()));
                log.setContent("银行："+record.getBankName());
                customerLogService.insert(log);
                return R.build(num);

        }catch (Exception e){
            e.printStackTrace();
            return   R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 更新
     * @param record
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public R<Integer> update(@RequestBody Customer2BankEntity record){
        try {
            UserEntity user = (UserEntity)this.getBean("user");
            int num = customer2BankService.updateByPrimaryKeySelective(record);

            //日志
            Customer2LogEntity log=new Customer2LogEntity();
            log.setCode(record.getCode());
            log.setType("更新");
            log.setOptioner(user.getCode());
            log.setOptionTime(DateUtils.format(new Date()));
            log.setContent("银行："+record.getBankName());
            customerLogService.insert(log);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return   R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 分页查询
     * @param code 供应商编码
     * @return
     */
    @RequestMapping(value = "selectByPage")
    @ResponseBody
    public R<List<Customer2BankEntity>> selectByPage(String code){
        try{
            List<Customer2BankEntity> customer2BankEntities=new ArrayList<>();
            if(code.equals("")){
                return R.build(customer2BankEntities);
            }
            Customer2BankEntityExample example=new Customer2BankEntityExample();
            Customer2BankEntityExample.Criteria criteria = example.createCriteria();
            criteria.andCodeEqualTo(code);
            customer2BankEntities = customer2BankService.selectByExample(example);
            return R.build(customer2BankEntities);
        }catch (Exception e){
            e.printStackTrace();
            return   R.buildError(ResultStatusEnum.ERROR);
        }
    }


}

